package com.atguigu.controller;

import com.alibaba.fastjson.JSON;
import com.atguigu.client.ProductFeignClient;
import com.atguigu.client.SearchFeignClient;
import com.atguigu.entity.BaseCategoryView;
import com.atguigu.entity.ProductSalePropertyKey;
import com.atguigu.entity.SkuInfo;
import com.atguigu.executor.MyExecutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

@Controller
public class WebSkuDetailController {
    @Autowired
    private ProductFeignClient productFeignClient;
    @Autowired
    private SearchFeignClient searchFeignClient;
    //http://item.gmall.com/36.html
    @RequestMapping("{skuId}.html")
    public String getSkuDetail(@PathVariable Long skuId, Model model){
        Map<String, Object> dataMap = new HashMap<>();
        //1.根据skuid获取sku的基本信息(skuInfo)
        CompletableFuture<SkuInfo> skuFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            dataMap.put("skuInfo",skuInfo);
            return skuInfo;
        });
        //2.根据skuid获取spu的销售属性
        CompletableFuture<Void> spuSalePropertyFuture = skuFuture.thenAcceptAsync(skuInfo -> {
            List<ProductSalePropertyKey> spuSalePropertyList = productFeignClient.getSpuSaleProperty(skuInfo.getProductId(), skuId);
            dataMap.put("spuSalePropertyList", spuSalePropertyList);
        }, MyExecutor.getInstance());

        //3.根据skuid获取sku的分类信息
        CompletableFuture<Void> categoryViewFuture = skuFuture.thenAcceptAsync(skuInfo -> {
            BaseCategoryView categoryView = productFeignClient.getCategoryView(skuInfo.getCategory3Id());
            dataMap.put("categoryView", categoryView);
        }, MyExecutor.getInstance());

        //4.根据skuid获取sku的实时价格信息
        CompletableFuture<Void> priceFuture = CompletableFuture.runAsync(() -> {
            BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
            dataMap.put("price",skuPrice);
        },MyExecutor.getInstance());

        //5.根据spuId获取该spu所对应的所有sku的销售属性值与sku的对应关系
        CompletableFuture<Void> salePropertyValueIdJsonFuture = skuFuture.thenAcceptAsync(skuInfo -> {
            Map salePropertyValueIdMap = productFeignClient.getSkuSalePropertyValueIdMapping(skuInfo.getProductId());
            //把map转换为json
            dataMap.put("salePropertyValueIdJson", JSON.toJSONString(salePropertyValueIdMap));
        }, MyExecutor.getInstance());

        //6.商品热度排名加一
//        CompletableFuture<Void> hotScoreFuture = CompletableFuture.runAsync(() -> {
//            searchFeignClient.incrHotScore(skuId);
//        }, MyExecutor.getInstance());


        //7.多任务组合 必须这五个全部执行完拿到数据之后才走 赋值给页面
        CompletableFuture.allOf(
                skuFuture,
                spuSalePropertyFuture,
                categoryViewFuture,
                priceFuture,
                salePropertyValueIdJsonFuture
                //hotScoreFuture
        ).join();

        model.addAllAttributes(dataMap);
        return "detail/index";
    }


//    @RequestMapping("{skuId}.html")
//    public String getSkuDetail(@PathVariable Long skuId, Model model){
//        Map<String, Object> dataMap = new HashMap<>();
//        //1.根据skuid获取sku的基本信息(skuInfo)
//        SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
//        dataMap.put("skuInfo",skuInfo);
//        //2.根据skuid获取spu的销售属性
//        Long productId = skuInfo.getProductId();
//        List<ProductSalePropertyKey> spuSalePropertyList = productFeignClient.getSpuSaleProperty(productId, skuId);
//        dataMap.put("spuSalePropertyList",spuSalePropertyList);
//        //3.根据skuid获取sku的实时价格信息
//        BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
//        dataMap.put("price",skuPrice);
//        //4.根据skuid获取sku的分类信息
//        Long category3Id = skuInfo.getCategory3Id();
//        BaseCategoryView categoryView = productFeignClient.getCategoryView(category3Id);
//        dataMap.put("categoryView",categoryView);
//        //5.根据spuId获取该spu所对应的所有sku的销售属性值与sku的对应关系
//        Map salePropertyValueIdMap = productFeignClient.getSkuSalePropertyValueIdMapping(productId);
//        //把map转换为json
//        dataMap.put("salePropertyValueIdJson",JSON.toJSONString(salePropertyValueIdMap));
//        model.addAllAttributes(dataMap);
//        return "detail/index";
//    }
}
